#!/usr/bin/python
#coding=utf8
import urllib
from PIL import Image
import sys,os,time
import tesseract

api = tesseract.TessBaseAPI()
api.Init('.','num4',tesseract.OEM_DEFAULT)
api.SetVariable("tessedit_char_whitelist", '0123456789')#abcdefghijklmnopqrstuvwxyz')
api.SetPageSegMode(tesseract.PSM_AUTO)
api.SetPageSegMode(8)
for n in xrange(50):
#     urllib.urlretrieve('http://www.jumei.com/i/account/hash_code?from=signup&1375331637','testdata\hash_code%d.png'%n)
    im = Image.open('testdata\hash_code%d.png'%n)
    im=im.resize((im.size[0]*2,im.size[1]*2),Image.NEAREST).convert('RGB')
    pixdata=im.load()
    R=169
    G=169
    B=169
    for y in xrange(0,im.size[1]):
        for x in xrange(0,im.size[0]):
            if (pixdata[x,y][0]>R and pixdata[x,y][1]>G and pixdata[x,y][2]>B):
                pixdata[x,y]=(255,255,255)
            else:
                pixdata[x,y]=(0,0,0)
    for y in xrange(1,im.size[1]-1):
        for x in xrange(1,im.size[0]-1):
            nearDots = 0 
            L=pixdata[x,y]
            if L == pixdata[x-1,y-1]:  
                nearDots += 1  
            if L == pixdata[x-1,y]:  
                nearDots += 1  
            if L == pixdata[x-1,y+1]:   
                nearDots += 1  
            if L == pixdata[x,y-1]:  
                nearDots += 1  
            if L == pixdata[x,y+1]:   
                nearDots += 1  
            if L == pixdata[x+1,y-1]: 
                nearDots += 1  
            if L == pixdata[x+1,y]:  
                nearDots += 1  
            if L == pixdata[x+1,y+1]:  
                nearDots += 1 
            if nearDots<=3:
                pixdata[x,y]=(255,255,255)
    for y in xrange(1,im.size[1]-1):
        for x in xrange(1,im.size[0]-1):
            nearDots = 0 
            L=pixdata[x,y]
            if L == pixdata[x-1,y-1]:  
                nearDots += 1  
            if L == pixdata[x-1,y]:  
                nearDots += 1  
            if L == pixdata[x-1,y+1]:   
                nearDots += 1  
            if L == pixdata[x,y-1]:  
                nearDots += 1  
            if L == pixdata[x,y+1]:   
                nearDots += 1  
            if L == pixdata[x+1,y-1]: 
                nearDots += 1  
            if L == pixdata[x+1,y]:  
                nearDots += 1  
            if L == pixdata[x+1,y+1]:  
                nearDots += 1 
            if nearDots<=3:
                pixdata[x,y]=(255,255,255)
    x=im.size[0]
    y=im.size[1]
    c = Image.new("RGB",(x,y),(255,255,255))
    for i in range (10,x-10):
        for j in range (10,y-10):
            c.putpixel([i,j],pixdata[i,j])
#     c.show()
    c.save('testdata\product%d.png'%n) 
    mImgFile = 'testdata\product%d.png'%n
    mBuffer=open(mImgFile,"rb").read()
    result = tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api)
    os.rename('C:\\Users\\Administrator\\workspace\\Image_Recognition\\src\\testdata\\product%d.png'%n,'C:\\Users\\Administrator\\workspace\\Image_Recognition\\src\\testdata\\%s.png'%result.strip())
    time.sleep(1)
#     os.remove('C:\\Users\\Administrator\\workspace\\Image_Recognition\\src\\testdata\\hash_code%d.png'%n)
#     time.sleep(1)





# from pytesser import *             
# img = Image.open('product5.png')    
# img.load()                                
# if len(img.split())==4:               
#     r,g,b,a = img.split()        
#     img = Image.merge("RGB",(r,g,b))    
# image_to_string(img)


# im = Image.open('product3.png')
# for i in [15,45,75,105]:
#     im.crop((i,0,i+10,im.size[1])).save('a%d.png'%i)


# im = Image.open('product5.png').convert("L")
# pixdata=im.load()
# flagx=[0 for x in xrange(im.size[0])]
# for x in xrange(im.size[0]):
#     for y in xrange(im.size[1]):
#         if pixdata[x,y]<90:
#             flagx[x]+=1
# print flagx
# result=[]
# for i in xrange(im.size[0]):
#     if flagx[i]>0 and flagx[i-1]<=0:
#         tmp=i
#     if flagx[i]>0 and flagx[i-1]<=0:
#         flagy=[0 for x in xrange(im.size[1])]
#         for y in xrange(im.size[1]):
#             for x in xrange(i+1)[tmp:]:
#                 if pixdata[x,y]<90:
#                     flagy[y]+=1
#         for x in xrange(im.size[1]):
#             if flagy[x]!=0:
#                 ttmp=x
#                 break
#         for x in xrange(im.size[1]-1,-1,-1):
#             if flagy[x]!=0:
#                 result.append([tmp,i+1,ttmp,x+1])
#                 break
# 
# im = Image.open('product3.png')
# for i in [15,45,75,105]:
#     im.crop((i,0,i+10,im.size[1])).save('a%d.png'%i)


